home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 11 - 1995 / 11.02 Feb 95 / Yenta / C Libraries / ToolboxTools.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-04  |  6.4 KB  |  243 lines  |  [TEXT/KAHL]

  1. #include <ToolboxTools.h>
  2. #include <OSUtils.h>        // for GetDateTime
  3. #include <BDC.h>            // for ToString
  4. #include <Menus.h>            // for SetItem
  5.  
  6. void ToString2 (long tempInt, register StringPtr tempString);
  7.  
  8. #define     kChooserName    -16096
  9.  
  10. /*------------------------------------------------------------------------------*/
  11.  
  12.     StringPtr GetChooserName (StringPtr Default)
  13.     /* return the name which the chooser contains as the user name */
  14.     {
  15.         StringHandle    ChooserName;
  16.         
  17.         ChooserName = (StringHandle)GetResource ('STR ', kChooserName);
  18.         if (!ChooserName)
  19.           return Default;
  20.         else
  21.           return *ChooserName;
  22.     }
  23.     
  24. /*------------------------------------------------------------------------------*/
  25.  
  26.     Boolean  ButtonStillDownAfterN (long NumTicks)
  27.     /* return TRUE if the mouse button is held down for longer than NumTicks */
  28.     /* 60'ths of a second; false if it is released before that */
  29.     {
  30.         long    StartTime;
  31.         StartTime = TickCount();
  32.         
  33.         while (StillDown())
  34.           if (TickCount() - StartTime > NumTicks)
  35.             return TRUE;
  36.         return FALSE;
  37.     }
  38.  
  39. /*------------------------------------------------------------------------------*/
  40.  
  41.     Boolean CommandKeyDown (short modifierKeys)
  42.     {
  43.         if (modifierKeys & 0x0100)
  44.           return TRUE;
  45.         else
  46.           return FALSE;
  47.     }
  48.     
  49. /*------------------------------------------------------------------------------*/
  50.  
  51.     Boolean    ShiftKeyDown (short modifierKeys)
  52.     {
  53.         if (modifierKeys & 0x0200)
  54.           return TRUE;
  55.         else
  56.           return FALSE;
  57.     }
  58.     
  59. /*------------------------------------------------------------------------------*/
  60.  
  61.     Boolean    OptionKeyDown (short modifierKeys)
  62.     {
  63.         if (modifierKeys & 0x0800)
  64.           return TRUE;
  65.         else
  66.           return FALSE;
  67.     }
  68.  
  69. /*------------------------------------------------------------------------------*/
  70.  
  71.     Boolean    ControlKeyDown (short modifierKeys)
  72.     {
  73.         if (modifierKeys & 0x1000)
  74.           return TRUE;
  75.         else
  76.           return FALSE;
  77.     }
  78.  
  79. /*------------------------------------------------------------------------------*/
  80.  
  81.     short isPressed(unsigned short k )
  82.     // k =  any keyboard scan code, 0-127
  83.     {
  84.         unsigned char km[16];
  85.     
  86.         GetKeys( (long *) km);
  87.         return ( ( km[k>>3] >> (k & 7) ) & 1);
  88.     }
  89.  
  90. /*------------------------------------------------------------------------------*/
  91.  
  92.     short getModifiers (void)
  93.     /* get the states of all the modifier keys on the keyboard and return them */
  94.     /* in ModKeys in the format used in the 'modifier' field of an event record */
  95.     {
  96.         short    ModKeys;
  97.         
  98.         ModKeys = 0;
  99.         if (isPressed(0x37))        // command key
  100.           ModKeys += 0x0100;
  101.         if (isPressed(0x38))        // shift key
  102.           ModKeys += 0x0200;
  103.         if (isPressed(0x39))        // caps-lock key
  104.           ModKeys += 0x0400;
  105.         if (isPressed(0x3A))        // option key
  106.           ModKeys += 0x0800;
  107.         if (isPressed(0x3B))        // control key
  108.           ModKeys += 0x1000;
  109.         return ModKeys;
  110.     }
  111.     
  112. /*------------------------------------------------------------------------------*/
  113.  
  114.     void ErrorAlert (short ErrorID, StringPtr Message)
  115.     /* this routine puts up a dialog which displays an error in one of the
  116.        following ways:
  117.        1) if there is an STR resource with the same ID as ErrorID, that
  118.              message is displayed along with the passed-in message
  119.        2) if there is no STR resource, the ErrorID and the passed-in
  120.           message are displayed.
  121.     */
  122.     {
  123.         Handle    ErrorMsg = Get1Resource ('STR ', ErrorID);
  124.         Str255    STemp, STemp2;
  125.         
  126.         if (ErrorMsg)
  127.           {
  128.               if (Message)
  129.                 PStrCat (254, STemp, 3, (StringPtr)(*ErrorMsg), "\p\r", Message);
  130.               else
  131.                 BlockMove (*ErrorMsg, STemp, (**ErrorMsg)+1);
  132.           }
  133.         else
  134.           {
  135.               NumToString (ErrorID, (StringPtr)(&STemp2));
  136.               if (Message)
  137.                 PStrCat (254, STemp, 4, "\pError # ", STemp2, "\p : \r", Message);
  138.               else
  139.                 PStrCat (254, STemp, 2, "\pError # ", STemp2);
  140.           }
  141.             
  142.         ParamText (STemp, NULL, NULL, NULL);
  143.         NoteAlert (-16394, NULL);        // use a system alert to show the message
  144.     }
  145.     
  146. /*------------------------------------------------------------------------------*/
  147.  
  148.     void TimeString (long inDateTime, Boolean showAMPM, StringPtr DestString, short maxLen)
  149.     {
  150.         DateTimeRec    tempDate;
  151.         unsigned char    min[3], hr[3], sec[3];
  152.         Str255        STemp;
  153.         char        C = 0;
  154.  
  155.         Secs2Date (inDateTime, &tempDate);
  156.         if (showAMPM)
  157.           {
  158.               if (tempDate.hour == 0)
  159.                 {
  160.                     tempDate.hour = 12;
  161.                   C = 'A';
  162.                 }
  163.             else if ((tempDate.hour >= 13) && (tempDate.hour <= 23))
  164.               {
  165.                   tempDate.hour = tempDate.hour - 12;
  166.                   C = 'P';
  167.               }
  168.             else
  169.               C = 'A';
  170.           }
  171.         
  172.         ToString2(tempDate.hour, hr);
  173.         ToString2(tempDate.minute, min);
  174.         ToString2(tempDate.second, sec);
  175.         
  176.         // Construct the basic time string
  177.         PStrCat (255, DestString, 5, hr, "\p:", min, "\p:", sec);
  178.         
  179.         // add 'AM' or 'PM' as needed
  180.         switch (C) {
  181.             case 'A' : PStrCat (255, DestString, 2, DestString, "\p AM");    break;
  182.             case 'P' : PStrCat (255, DestString, 2, DestString, "\p PM");    break;
  183.         }
  184.     }
  185.  
  186. /*------------------------------------------------------------------------------*/
  187.  
  188.     void CurrentTimeString (Boolean showAMPM, StringPtr DestString, short maxLen)
  189.     {
  190.         unsigned long    InDateTime;
  191.         
  192.         GetDateTime(&InDateTime);
  193.         TimeString(InDateTime, showAMPM, DestString, maxLen);
  194.     }
  195.  
  196. /*------------------------------------------------------------------------------*/
  197.  
  198.     void ToString2 (long tempInt, register StringPtr tempString)
  199.     /* a special routine which takes an integer, converts it to a string */
  200.     /* and then, pads it with zeroes on the left or truncates characters on */
  201.     /* the right to make sure it is exactly 2 characters long */
  202.     {
  203.         NumToString(tempInt, tempString);
  204.         switch (tempString[0]) {
  205.             case 0 : tempString[1] = tempString[2] = '0';
  206.             case 1 : tempString[2] = tempString[1];
  207.                      tempString[1] = '0';
  208.         }
  209.         tempString[0] = 2;
  210.     }
  211.  
  212. /*------------------------------------------------------------------------------*/
  213.  
  214.     pascal void SetMenuItemText(MenuHandle theMenu,short item,StringPtr itemString)
  215.     {
  216.         SetItem(theMenu, item,itemString);
  217.     }
  218.  
  219. /*-----------------------------------------------------------------*/
  220.  
  221.     void CopyResource (short RFrom, short RTo, ResType itsType, short itsID)
  222.     /* copy the specified resource from the resource file specified by */
  223.     /* RFrom into the resource file specified by RTo */
  224.     {
  225.         Str255    itsName;
  226.         Handle    HTemp = NULL;
  227.         short    RCurrent = CurResFile();
  228.         
  229.         UseResFile (RFrom);
  230.         HTemp = Get1Resource (itsType, itsID);
  231.         if (HTemp)
  232.           {
  233.               GetResInfo (HTemp, &itsID, &itsType, itsName);
  234.               DetachResource (HTemp);
  235.               UseResFile(RTo);
  236.               AddResource (HTemp, itsType, itsID, itsName);
  237.               WriteResource (HTemp);
  238.           }
  239.         
  240.         UseResFile (RCurrent);
  241.     }
  242.  
  243.